<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Cookie;
use think\Db;
class Index extends Controller
{
    public function index()
    {
        if(ismobile()){
               return $this->redirect('index/phone/index');
        }
        
    	if(empty(Session::get('tel'))){
    		if(!empty(Cookie::get('tel','job_'))){
                if(Db::name('user')->where('user_tel',Session::get('tel'))->find()){
                    Session::set('tel',Cookie::get('tel','job_'));
                }			
    		}
    	}

    	$city = Db::name('category')->field('id,name')->where('pid',3)->select();
    	$area = Db::name('category')->field('id,name')->where('pid',20)->select();
    	$property = Db::name('category')->field('id,name')->where('pid',1)->select();
    	$type = Db::name('category')->field('id,name')->where('pid',2)->select();
    	$position = Db::name('position')
			->field('position_id,position_name,position_companyid,position_place,position_num,position_start,position_end,position_publish,position_require,position_payway')
			->paginate(10);
        $cname = array(); 
        foreach($position->items() as $k=>$v){
            $companyname = Db::name('company_information')->field('company_name')->where('company_cid',$v['position_companyid'])->find();
            // dump($companyname);
            array_push($cname,$companyname['company_name']);
        }
        // dump($position->items());die;
        $this->assign('companyname',$cname);
    	$this->assign('citytitle','上海');
    	$this->assign('cityid',20);
    	$this->assign('city',$city);
    	$this->assign('area',$area);
    	$this->assign('property',$property);
    	$this->assign('type',$type);
    	$this->assign('position',$position);
    	$empty = "<div style='font-size:30px;color:red;height:200px;width:1000px;line-height:200px;text-align:center;'>暂无职位,去别处看看...</div>";
 		$this->assign('isselect',array(0,0,0));   	
    	$this->assign('empty',$empty);
        return $this->fetch('index/index');
    }
    

    public function switchcity($id)
    {
    	$city = Db::name('category')->field('id,name')->where('pid',3)->select();
    	$area = Db::name('category')->field('id,name')->where('pid',$id)->select();
    	$property = Db::name('category')->field('id,name')->where('pid',1)->select();
    	$type = Db::name('category')->field('id,name')->where('pid',2)->select();
    	$citytitle = Db::name('category')->field('id,name')->where('id',$id)->find();
    	$position = Db::name('position')
			->field('position_id,position_name,position_companyid,position_place,position_num,position_start,position_end,position_publish,position_require,position_payway')
			->paginate(10);
        $cname = array(); 
        foreach($position->items() as $k=>$v){
            $companyname = Db::name('company_information')->field('company_name')->where('company_cid',$v['position_companyid'])->find();
            // dump($companyname);
            array_push($cname,$companyname['company_name']);
        }
        $this->assign('companyname',$cname);
    	$this->assign('citytitle',$citytitle['name']);
    	$this->assign('cityid',$citytitle['id']);
    	$this->assign('city',$city);
    	$this->assign('area',$area);
    	$this->assign('property',$property);
    	$this->assign('type',$type);
    	$this->assign('position',$position);
    	$empty = "<div style='font-size:30px;color:red;height:200px;width:1000px;line-height:200px;text-align:center;'>暂无职位,去别处看看...</div>";
    	$this->assign('isselect',array(0,0,0));
    	$this->assign('empty',$empty);
        return $this->fetch('index/index');
    }


    //不同分类拿不同数据id 
    public function cateFind()
    {
    	// 4,9,35,
        $data = input('post.');
        $ids = rtrim($data['ids'],'B');
        $ids = str_replace('B',',',$ids);
        $res = Db::name('position')->field('position_id')->where('position_cateid',$ids)->select();
    	if($res){
    		$result = array_column($res, 'position_id');
    		$str = implode('B',$result);
    		return json($str);
    	}
    }

    //真拿
    public function findPosition()
    {
        $all = input('get.all');
    	$data = explode('A',$all);
        $data[0] = str_replace('B',',',$data[0]);
    	$position = Db::name('position')
    				->field('position_id,position_name,position_companyid,position_place,position_num,position_start,position_end,position_publish,position_require,position_payway')
                    ->where('position_id','in',$data[0])
    				->paginate(10);
        $cname = array(); 
        foreach($position->items() as $k=>$v){
            $companyname = Db::name('company_information')->field('company_name')->where('company_cid',$v['position_companyid'])->find();
            // dump($companyname);
            array_push($cname,$companyname['company_name']);
        }
        $this->assign('companyname',$cname);
    	$sel = explode('B',rtrim($data[2],'B'));
    	$property = Db::name('category')->field('id,name')->where('pid',1)->select();
    	$type = Db::name('category')->field('id,name')->where('pid',2)->select();
    	$citytitle = Db::name('category')->field('id,name')->where('id',$data[1])->find();
    	$this->assign('citytitle',$citytitle['name']);
    	$this->assign('cityid',$citytitle['id']);
    	$city = Db::name('category')->field('id,name')->where('pid',3)->select();
    	$area = Db::name('category')->field('id,name')->where('pid',$data[1])->select();
    	$this->assign('position',$position);
    	$this->assign('city',$city);
    	$this->assign('area',$area);
    	$this->assign('type',$type);
    	$this->assign('property',$property);
    	$this->assign('isselect',$sel);
    	$empty = "<div style='font-size:30px;color:red;height:200px;width:1000px;line-height:200px;text-align:center;'>暂无职位,去别处看看...</div>";
    	$this->assign('empty',$empty);
    	return view('index/index');
    } 


    public function message($id)
    {
        $city = Db::name('category')->field('id,name')->where('pid',3)->select();
        $position = Db::name('position')
                    ->field('position_id,position_cateid,position_tel,position_apply,position_id,position_name,position_companyid,position_place,position_num,position_start,position_end,position_publish,position_require,position_payway')
                    ->find($id);

        $data = explode(',',$position['position_cateid']);
        $type = Db::name('category')->field('name')->find($data[1]);
        $place = Db::name('category')->field('name')->find($data[2]);
        $this->assign('cityid',20);
        $this->assign('citytitle','上海');
        $this->assign('city',$city);
        $this->assign('list',$position);
        $this->assign('type',$type['name']);
        $this->assign('place',$place['name']);
       
        //职位是否收藏过
        $usertel = Session::get('tel');
        $usertel = substr($usertel,0,11);
        $uid = Db::name('user')->field('user_id')->where('user_tel',$usertel)->find();
        $iscollect = Db::name('user_position')->where(['uid'=>$uid['user_id'],'pid'=>$position['position_id']])->find();;
        $status = empty($iscollect)?'2':'1';

        // 是否关注公司
        $cid = Db::name('position')->field('position_companyid')->where('position_companyid',$id)->find();
        $attention1 = Db::name('user_company')
                        ->where(['uid'=>$uid['user_id'],'cid'=>$cid['position_companyid'],'status'=>1])
                        ->find();
         $attention2 = Db::name('user_company')
                    ->where(['uid'=>$uid['user_id'],'cid'=>$cid['position_companyid'],'status'=>3])
                    ->find();
        
        if(empty($attention1) && empty($attention2)){
            $att = 2;
        }else{
            $att = 1;
        }
        //获取公司数据
        $this->assign('iscollect',$status);
        $this->assign('collectcompany',$att);

        // dump($id);die;
        $company = Db::name('company_information')->where('company_id',$id)->find();
        $this->assign('company',$company);
        $other = Db::name('position')->field('position_name,position_id,position_payway')->where('position_name','like','%'.$position["position_name"].'%')->limit(10)->select();
        $this->assign('other',$other);
        return $this->fetch('index/message');
    }


    //收集职位
    public function collect()
    {
        $usertel = Session::get('tel');
        $usertel = substr($usertel,0,11);
        if(empty($usertel)){
            $arr = ['code'=>0,'msg'=>'未登录'];
        }else if(Session::get('usercate')==2){
            $arr = ['code'=>0,'msg'=>'您是企业用户,不能关注'];
        }else{
            $data = input('post.');     
            $uid = Db::name('user')->field('user_id')->where('user_tel',$usertel)->find();
            $collectdata['uid'] = $uid['user_id'];
            $collectdata['pid'] = $data['job_id'];
            if($data['iscollect']==1){
                //添加收藏 
                if(DB::name('user_position')->insert($collectdata)){
                   $arr = ['code'=>1,'msg'=>'已收藏'];
                } 
            }else{
                if(Db::name('user_position')->where(['uid'=>$uid['user_id'],'pid'=>$data['job_id']])->delete()){
                    $arr = ['code'=>1,'msg'=>'已取消'];
                }
            }
        }
        return json($arr);
    }

    //关注公司
    public function collectcompany()
    {
        $usertel = Session::get('tel');
        $usertel = substr($usertel,0,11);
        if(empty($usertel)){
            $arr = ['code'=>0,'msg'=>'未登录'];
        }else if(Session::get('usercate')==2){
            $arr = ['code'=>0,'msg'=>'您是企业用户,不能关注'];
        }else{
            $data = input('post.');      
            $uid = Db::name('user')->field('user_id')->where('user_tel',$usertel)->find();
            $collectdata['uid'] = $uid['user_id'];
            $collectdata['cid'] = $data['c_id'];
            $collectdata['status'] = 1;
            if($data['collectcompany']==1){
                //添加收藏 
                if(Db::name('user_company')->where(['uid'=>$uid['user_id'],'cid'=>$collectdata['cid'],'status'=>2])->find()){
                    if(Db::name('user_company')->where(['uid'=>$uid['user_id'],'cid'=>$collectdata['cid'],'status'=>2])->update(['status'=>3])){
                        $arr = ['code'=>1,'msg'=>'已收藏'];
                    }else{
                        $arr = ['code'=>0,'msg'=>'关注失败'];
                    }
                }elseif(DB::name('user_company')->insert($collectdata)){
                   $arr = ['code'=>1,'msg'=>'已收藏'];
                } else{
                    $arr = ['code'=>0,'msg'=>'关注失败'];
                }
            }else{
              if(Db::name('user_company')->where(['uid'=>$uid['user_id'],'cid'=>$collectdata['cid'],'status'=>3])->find()){
                    if(Db::name('user_company')->where(['uid'=>$uid['user_id'],'cid'=>$collectdata['cid'],'status'=>3])->update(['status'=>2])){
                        $arr = ['code'=>1,'msg'=>'已取消收藏'];
                    }
              }else if(Db::name('user_company')->where(['uid'=>$uid['user_id'],'cid'=>$collectdata['cid']])->delete()){
                    $arr = ['code'=>1,'msg'=>'已取消'];
                }
            }
        }
        return json($arr);
    }

    //显示立即报名
    public function showApply()
    {
        $pid = input('get.pid');
        $this->assign('pid',$pid);
        return $this->fetch('company/apply');
    }

    public function apply($pid)
    {
        $data = input('post.');
        $usertel = Session::get('tel');
        $usertel = substr($usertel,0,11);
        $uid = Db::name('user')->field('user_id')->where('user_tel',$usertel)->find();
        if($data['name']==''){
            echo "<script>if(confirm('用户名不能为空'))";
            echo "location.href='".$_SERVER["HTTP_REFERER"]."';else location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
        }

        if(!preg_match('/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/',$data['tel'])){
            echo "<script>if(confirm('请输入正确的电话'))";
            echo "location.href='".$_SERVER["HTTP_REFERER"]."';else location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
        }

        if($data['address']==''){
            echo "<script>if(confirm('地址不能为空'))";
            echo "location.href='".$_SERVER["HTTP_REFERER"]."';else location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
        }

        $cid = Db::name('position')->field('position_companyid')->where('position_id',$pid)->find();
        $insert['uid'] = $uid['user_id'];
        $insert['pid'] = $pid; 
        $insert['cid'] = $cid['position_companyid'];
        $insert['tel'] = $data['tel'];
        $insert['name'] = $data['name'];
        $insert['address'] = $data['address'];
        $insert['addtime'] = time();
        if(Db::name('apply')->where(['uid'=>$uid['user_id'],'pid'=>$pid])->find()){
             echo '<script>alert("您已经报名了该职位,请到个人信息查看");window.close();</script>';
        }else{
            if(Db::name('apply')->insert($insert)){
                echo '<script>alert("报名成功");window.close();</script>';
            }else{
                echo '<script>alert("报名失败,请稍后再试...");window.close();</script>';
            }
        }

    }
}


